﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClubVIPPoints
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string getPageSource(string URL)
        {
            System.Net.WebClient webClient = new System.Net.WebClient();
            string strSource = webClient.DownloadString(URL);
            webClient.Dispose();
            return strSource;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            txtSource.Text = getPageSource("http://clubvip.jtsoftware.co.za/Keywords.aspx");

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser2.ScriptErrorsSuppressed = true;
            notifyIcon1.Icon = Properties.Resources.Activated;
            timer1.Interval = 1000 * 60 * 15; //15 Minutes
            timer1.Enabled = true;
            timer2.Interval = 1000 * 60 * Properties.Settings.Default.BuyNowTimer;
            timer2.Enabled = false;
            this.ShowInTaskbar = false;
            Properties.Settings.Default.BuyNowCount = 0;
            Hide();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (timer1.Enabled == true)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                turnOffTimerToolStripMenuItem.Checked = true;
                notifyIcon1.Icon = Properties.Resources.Deactivated;
                notifyIcon1.Text = "Club Vip Polling Deactivated...";
            }
            else if (timer1.Enabled == false)
            {
                timer1.Enabled = true;
                timer2.Enabled = true;
                turnOffTimerToolStripMenuItem.Checked = false;
                notifyIcon1.Icon = Properties.Resources.Activated;
                notifyIcon1.Text = "Club Vip Polling Active...";
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool completed = false;
            bool newKeyword = false;
            string httpSource = "";
            

            try
            {
                httpSource = getPageSource("http://clubvip.jtsoftware.co.za/Keywords.aspx");
                notifyIcon1.Icon = Properties.Resources.Activated;
                notifyIcon1.Text = "Club Vip Polling Active...";
            }
            catch
            {
                //timer1.Enabled = false;
                //making an auto alive where it will keep checking every hour to see if the site is up.
                timer1.Interval = 1000 * 60 * 60; // 60 minutes
                timer1.Enabled = true;
                timer2.Enabled = false;
                notifyIcon1.Icon = Properties.Resources.Deactivated;
                notifyIcon1.Text = "Club Vip Polling Deactivated...";

                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.BalloonTipTitle = "Website Down";
                notifyIcon1.BalloonTipText = "Keyword Website seems to be down.\nClubVIP Polling will now deactivate.";
                notifyIcon1.ShowBalloonTip(7000);

                return;

            }
            
            //static value to test.
            //string httpSource = "\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n<head><title>\r\n\t94.7 Highveld Stereo Club VIP Keywords - Latest Keywords\r\n</title><link rel=\"shortcut icon\" href=\"favicon.ico\" type=\"image/x-icon\" /><link href=\"ClubVIP.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n  <meta name=\"Description\" content=\"94.7 Highveld Stereo Club VIP Keywords - Latest Keywords\" />\r\n  <meta name=\"Keywords\" content=\"94.7,Club VIP,ClubVIP,Highveld Stereo,Keywords,Submit,Earn,Burn,Win,Rude Awakening,Jeremy Mansfield,Alex Jay,prize,competition,lucky,leech,free,Joburg,enter,points,track,miss,keywords\" />\r\n</head>\r\n<body>\r\n  <form name=\"aspnetForm\" method=\"post\" action=\"Keywords.aspx\" id=\"aspnetForm\">\r\n<input type=\"hidden\" name=\"ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField\" id=\"ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField\" value=\"\" />\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/wEPDwUKMTAxMDA3NzMyNQ9kFgJmD2QWAgIDD2QWBAIBDxYCHgdWaXNpYmxlaGQCAw9kFgICBQ9kFgJmD2QWAgIDDzwrAAsBAA8WCB4IRGF0YUtleXMWAB4LXyFJdGVtQ291bnQCCh4JUGFnZUNvdW50AkUeFV8hRGF0YVNvdXJjZUl0ZW1Db3VudAKwBWQWAmYPZBYUAgIPDxYGHglGb3JlQ29sb3IKpAEeCUJhY2tDb2xvcgpPHgRfIVNCAgxkFgpmDw8WAh4EVGV4dAUEMTEwOGRkAgEPDxYCHwgFA3RhcGRkAgIPDxYCHwgFETE4IEphbiAyMDExIDE1OjMwZGQCAw8PFgIfCAUDMTIwZGQCBA8PFgIfCAUERWFybmRkAgMPZBYKZg8PFgIfCAUEMTEwNmRkAgEPDxYCHwgFA2hvdGRkAgIPDxYCHwgFETE4IEphbiAyMDExIDExOjMwZGQCAw8PFgIfCAUDMTMwZGQCBA8PFgIfCAUERWFybmRkAgQPZBYKZg8PFgIfCAUEMTEwM2RkAgEPDxYCHwgFBHNlbmRkZAICDw8WAh8IBRExOCBKYW4gMjAxMSAwODozMGRkAgMPDxYCHwgFAzE1MGRkAgQPDxYCHwgFBEVhcm5kZAIFD2QWCmYPDxYCHwgFBDExMDFkZAIBDw8WAh8IBQRmYWlsZGQCAg8PFgIfCAURMTcgSmFuIDIwMTEgMTg6MzBkZAIDDw8WAh8IBQMxNTBkZAIEDw8WAh8IBQRFYXJuZGQCBg9kFgpmDw8WAh8IBQQxMTAwZGQCAQ8PFgIfCAUEc2tpbmRkAgIPDxYCHwgFETE3IEphbiAyMDExIDE0OjMwZGQCAw8PFgIfCAUDMTIwZGQCBA8PFgIfCAUERWFybmRkAgcPZBYKZg8PFgIfCAUEMTA5OWRkAgEPDxYCHwgFA3BlbmRkAgIPDxYCHwgFETE3IEphbiAyMDExIDExOjMwZGQCAw8PFgIfCAUDMTMwZGQCBA8PFgIfCAUERWFybmRkAggPZBYKZg8PFgIfCAUEMTA5OGRkAgEPDxYCHwgFBGJhbmRkZAICDw8WAh8IBRExMyBKYW4gMjAxMSAxNTowMGRkAgMPDxYCHwgFAzE1MGRkAgQPDxYCHwgFBEVhcm5kZAIJD2QWCmYPDxYCHwgFBDEwOTdkZAIBDw8WAh8IBQRjYXJlZGQCAg8PFgIfCAURMTIgSmFuIDIwMTEgMTc6MzBkZAIDDw8WAh8IBQMxMzBkZAIEDw8WAh8IBQRFYXJuZGQCCg9kFgpmDw8WAh8IBQQxMDk1ZGQCAQ8PFgIfCAUEYmFieWRkAgIPDxYCHwgFETA5IERlYyAyMDEwIDE3OjMwZGQCAw8PFgIfCAUDMTMwZGQCBA8PFgIfCAUERWFybmRkAgsPZBYKZg8PFgIfCAUEMTA5NGRkAgEPDxYCHwgFBGdpcmxkZAICDw8WAh8IBREwOSBEZWMgMjAxMCAxNTozMGRkAgMPDxYCHwgFAzEyMGRkAgQPDxYCHwgFBEVhcm5kZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUjY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRpYlJlZnJlc2jeohsf0bkNEIgSV4LeTbA7uw5E6QexIvPeQ/0P0UUO1A==\" />\r\n\r\n\r\n<script src=\"/ClubVIP/ScriptResource.axd?d=RTHBHe4Od15ZSoc3mB-0juWV63_3-653lZ9xQb5fJZ-NmjIXcPzrEkV2yq5L-RcLgF1mEeRtEG_EToku26M-7lLGoZ2S3dhpKmo6goH8n7ZgKI7njjMGxobAq2vXjXXYtrbh7LLNfT3rDR7opFBlvQ2&amp;t=ffffffffd6f14dc5\" type=\"text/javascript\"></script>\r\n<script type=\"text/javascript\">\r\n//<![CDATA[\r\nif (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');\r\n//]]>\r\n</script>\r\n\r\n<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"/wEWAwKElbDPCgLzk6OkDQLOsJGtCn2tj+2PT5KFcibg67WefTMq2ehkWJyq9WeWzIY0IQ3T\" />\r\n    <div>\r\n      <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n        <tr>\r\n          <td colspan=\"4\" height=\"10\"></td>\r\n        </tr>\r\n        <tr>\r\n          <td width=\"30\"></td>\r\n          <td valign=\"top\"><img src=\"images/clubvip.jpg\" width=\"50\" height=\"50\" /><br /><br />\r\n            <a href=\"Home.aspx\" class=\"MnuLink\">Home</a><br />\r\n            <a href=\"Keywords.aspx\" class=\"MnuLink\">Latest Keywords</a><br />\r\n            <a href=\"Submit.aspx\" class=\"MnuLink\">Submit a keyword</a><br />\r\n            <a href=\"Contact.aspx\" class=\"MnuLink\">Contact Us</a><br />\r\n            \r\n            <br />\r\n            <script type=\"text/javascript\"><!--\r\n            google_ad_client = \"pub-3660333051145843\";\r\n            /* Club VIP Web 120x240 */\r\n            google_ad_slot = \"0361624098\";\r\n            google_ad_width = 120;\r\n            google_ad_height = 240;\r\n            //-->\r\n            </script>\r\n            <script type=\"text/javascript\"\r\n            src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\r\n            </script></td>\r\n          <td width=\"20\"></td>\r\n          <td valign=\"top\">\r\n            \r\n  \r\n  <div id=\"ctl00_ContentPlaceHolder1_uprKeywords\" style=\"display:none;\">\r\n\t<div class=\"UpdateWait\"></div>\r\n</div>\r\n  <div id=\"ctl00_ContentPlaceHolder1_upKeywords\">\r\n\t\r\n  <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"400\">\r\n    <tr>\r\n      <td>\r\n      <h1>94.7 Highveld Stereo Club VIP - Latest Keywords</h1>\r\n      <p>The website stores keywords aired on 94.7 Highveld Stereo and posted on the <a href=\"Submit.aspx\" class=\"StdLink\">Keyword Submission page</a>.</p>\r\n      <p>Below is a list of the latest keywords. There are 10 keywords per page and you can use the paging links at the bottom to view older keywords.</p>\r\n      <p>Please remember that you can also view this website on your mobile phone.</p>\r\n       <br /></td>\r\n      <td align=\"right\" valign=\"bottom\"><input type=\"image\" name=\"ctl00$ContentPlaceHolder1$ibRefresh\" id=\"ctl00_ContentPlaceHolder1_ibRefresh\" src=\"images/refresh.gif\" alt=\"Refresh\" border=\"0\" /></td>\r\n    </tr>\r\n    <tr>\r\n      <td colspan=\"2\">\r\n        <table cellspacing=\"0\" cellpadding=\"2\" rules=\"all\" bordercolor=\"LightGrey\" border=\"1\" id=\"ctl00_ContentPlaceHolder1_dgKeywords\" width=\"400\">\r\n\t\t<tr class=\"Heading\">\r\n\t\t\t<td>Keyword</td><td>Expires</td><td>Points</td><td>Earn/Burn</td>\r\n\t\t</tr><tr bgcolor=\"Green\">\r\n\t\t\t<td><font color=\"White\">tap</font></td><td><font color=\"White\">18 Jan 2011 15:30</font></td><td><font color=\"White\">120</font></td><td><font color=\"White\">Earn</font></td>\r\n\t\t</tr><tr bgcolor=\"LightGrey\">\r\n\t\t\t<td>hot</td><td>18 Jan 2011 11:30</td><td>130</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"White\">\r\n\t\t\t<td>send</td><td>18 Jan 2011 08:30</td><td>150</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"LightGrey\">\r\n\t\t\t<td>fail</td><td>17 Jan 2011 18:30</td><td>150</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"White\">\r\n\t\t\t<td>skin</td><td>17 Jan 2011 14:30</td><td>120</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"LightGrey\">\r\n\t\t\t<td>pen</td><td>17 Jan 2011 11:30</td><td>130</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"White\">\r\n\t\t\t<td>band</td><td>13 Jan 2011 15:00</td><td>150</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"LightGrey\">\r\n\t\t\t<td>care</td><td>12 Jan 2011 17:30</td><td>130</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"White\">\r\n\t\t\t<td>baby</td><td>09 Dec 2010 17:30</td><td>130</td><td>Earn</td>\r\n\t\t</tr><tr bgcolor=\"LightGrey\">\r\n\t\t\t<td>girl</td><td>09 Dec 2010 15:30</td><td>120</td><td>Earn</td>\r\n\t\t</tr><tr>\r\n\t\t\t<td colspan=\"4\"><span>&lt;</span>&nbsp;<a href=\"javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$dgKeywords$ctl14$ctl01&#39;,&#39;&#39;)\">&gt;</a></td>\r\n\t\t</tr>\r\n\t</table>\r\n      </td>\r\n    </tr>\r\n  </table>\r\n  \r\n</div>\r\n\r\n          </td>\r\n        </tr>\r\n      </table>\r\n    </div>\r\n  \r\n\r\n<script type=\"text/javascript\">\r\n//<![CDATA[\r\n(function() {var fn = function() {$get('ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField').value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();//]]>\r\n</script>\r\n</form>\r\n<script type=\"text/javascript\">\r\nvar gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");\r\ndocument.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));\r\n</script>\r\n<script type=\"text/javascript\">\r\ntry {\r\nvar pageTracker = _gat._getTracker(\"UA-9125812-7\");\r\npageTracker._trackPageview();\r\n} catch(err) {}</script>\r\n</body>\r\n</html>\r\n";

            int startIndex = httpSource.IndexOf("<tr class=\"Heading\">");
            int endIndex = httpSource.IndexOf("<td colspan=\"4\"><span>&lt;</span>&nbsp;");
            int stringLength = endIndex - startIndex;

            if ((startIndex == -1) || (endIndex == -1))
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                notifyIcon1.Icon = Properties.Resources.Deactivated;
                notifyIcon1.Text = "Club Vip Polling Deactivated...";

                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.BalloonTipTitle = "Website Down";
                notifyIcon1.BalloonTipText = "Keyword Website seems to be unreachable or modified.\nPlease contact the administrator.\nClubVIP Polling will now deactivate.";
                notifyIcon1.ShowBalloonTip(7000);

                return;
            }

            string tableSource = httpSource.Substring(startIndex, stringLength);


            int startKeywordIndex;
            int endKeywordIndex;
            int keywordLength;
            int endPointIndex;
            int endUsageIndex;
            string usage = "";
            string point = "";
            string keyword = "";

            while (completed == false)
            {
                //keyword

                Properties.Settings.Default.Count = 0;

                if (tableSource.StartsWith("<tr bgcolor=\"Green\""))
                {
                    tableSource = tableSource.Substring(29, tableSource.Length - 29);
                    newKeyword = true;
                }
                else if (tableSource.StartsWith("<tr bgcolor=\"Orange\""))
                {
                    tableSource = tableSource.Substring(30, tableSource.Length - 30);
                    newKeyword = true;
                
                }
                else if (tableSource.StartsWith("<tr bgcolor=\"Red\""))
                {
                    tableSource = tableSource.Substring(27, tableSource.Length - 27);
                    newKeyword = true;
                
                }

                if (tableSource.StartsWith("<font color=\"White\">"))
                {
                    tableSource = tableSource.Substring(20, tableSource.Length - 20);
                    endKeywordIndex = tableSource.IndexOf("</td>");
                    keyword = tableSource.Substring(0, endKeywordIndex);
                }
                else
                {
                    startKeywordIndex = tableSource.IndexOf("<td>");
                    endKeywordIndex = tableSource.IndexOf("</td>");
                    keywordLength = endKeywordIndex - startKeywordIndex;
                    keyword = tableSource.Substring(startKeywordIndex + 4, keywordLength - 4);
                }                

                if (keyword.EndsWith("</font>"))
                {
                    keyword = keyword.Substring(0, keyword.Length - 7);
                }

                keywordLength = keyword.Length + 4;

                //removing keyword from string
                startIndex = tableSource.IndexOf("</td>");
                endIndex = tableSource.Length;
                stringLength = endIndex - startIndex - keywordLength - 5;
                tableSource = tableSource.Substring(startIndex + 9, stringLength - 5);

                //removing expires from string
                startIndex = tableSource.IndexOf("</td>");
                endIndex = tableSource.Length;
                stringLength = endIndex - startIndex - keywordLength - 5;
                tableSource = tableSource.Substring(startIndex + 9, stringLength - 5);




                //points

                if (tableSource.StartsWith("<font color=\"White\">"))
                {
                    tableSource = tableSource.Substring(20, tableSource.Length - 20);
                    endKeywordIndex = tableSource.IndexOf("</td>");
                    point = tableSource.Substring(0, endKeywordIndex);
                }
                else
                {
                    endPointIndex = tableSource.IndexOf("</td>");
                    point = tableSource.Substring(0, endPointIndex);
                }

                if (point.EndsWith("</font>"))
                {
                    point = point.Substring(0, point.Length - 7);
                }

                //removing points from string
                startIndex = tableSource.IndexOf("</td>");
                endIndex = tableSource.Length;
                stringLength = endIndex - startIndex - keywordLength;
                tableSource = tableSource.Substring(startIndex + 9, stringLength - 5);



                //usage

                if (tableSource.StartsWith("<font color=\"White\">"))
                {
                    tableSource = tableSource.Substring(20, tableSource.Length - 20);
                    endKeywordIndex = tableSource.IndexOf("</td>");
                    usage = tableSource.Substring(0, endKeywordIndex);
                }
                else
                {
                    endUsageIndex = tableSource.IndexOf("</td>");
                    usage = tableSource.Substring(0, endUsageIndex);
                }

                if (usage.EndsWith("</font>"))
                {
                    usage = usage.Substring(0, usage.Length - 7);
                }




                if (keyword == "Keyword")
                {
                    startIndex = tableSource.IndexOf("</tr>");
                    endIndex = tableSource.Length;
                    stringLength = endIndex - startIndex;
                    tableSource = tableSource.Substring(startIndex + 5, stringLength - 5);
                }
                else
                {
                    if (Properties.Settings.Default.Keyword == "")
                    {
                        Properties.Settings.Default.Keyword = keyword;
                        newKeyword = false;
                    }
                    else if (Properties.Settings.Default.Keyword == keyword)
                    {
                        newKeyword = false;
                    }
                    else
                    {
                        Properties.Settings.Default.Keyword = keyword;
                        newKeyword = true;
                    }
                    completed = true;
                }

            }

            if (newKeyword)
            {
                Properties.Settings.Default.Save();
                
                if (usage == "Earn")
                {
                    notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                    notifyIcon1.BalloonTipTitle = "Keyword Submitted";
                    notifyIcon1.BalloonTipText = "The New Keyword is: " + Properties.Settings.Default.Keyword + "\nPoints:" + point;
                    notifyIcon1.ShowBalloonTip(10000);
                }
                else if (usage == "Burn")
                {
                    MessageBox.Show(usage + " Points" + "\nThe New Keyword is: " + Properties.Settings.Default.Keyword +
                "\nPoints:" + point, "Burn Keyword", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                
                if (usage == "Earn")
                {
                    webBrowser1.AllowNavigation = true;

                    webBrowser1.Navigate("http://www.clubvip.co.za");
                }
                               
             }
        }

        private void itmImmediateCheck_Click(object sender, EventArgs e)
        {
            timer1_Tick(this, e);
        }

        private void itmExit_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            Application.Exit();
        }

        private void itmUsernamePassword_Click(object sender, EventArgs e)
        {
            Form usernamePassword = new UsernamePassword();
            usernamePassword.Show();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (Properties.Settings.Default.Count == 0)
            {
                HtmlElement browserUsername = webBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_CellNumberRadText");
                browserUsername.InnerText = Properties.Settings.Default.Username;

                HtmlElement browserPassword = webBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_PasswordRadText");
                browserPassword.InnerText = Properties.Settings.Default.Password;

                HtmlElement browserKeyword = webBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_KeyWordRadText");
                browserKeyword.InnerText = Properties.Settings.Default.Keyword;

                HtmlElement browserLoginButton = webBrowser1.Document.GetElementById("ctl00_ContentPlaceHolder1_cmdLogin");
                browserLoginButton.InvokeMember("click");

                Properties.Settings.Default.Count = Properties.Settings.Default.Count + 1;
            }

        }

        private void itmBuyNow_Click(object sender, EventArgs e)
        {

            Properties.Settings.Default.CheckBuyNowStatus = true;

            checkBuyNowStatus();

            


            //This is now checkBuyNowStatus method.

            /*try
            {
                webBrowser2.AllowNavigation = true;

                webBrowser2.Navigate("http://www.clubvip.co.za/Members/Shop.aspx");
            }
            catch
            {
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.BalloonTipTitle = "Shop Website Error";
                notifyIcon1.BalloonTipText = "There seems to be an error with the Shop Website.\nPlease contact the administrator.\nShop Polling will now deactivate.";
                notifyIcon1.ShowBalloonTip(7000);
                timer2.Enabled = false;
                return;
            }*/

        }

        private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            string shopSource = "";
            bool checkBuyNow = false;

            if ((webBrowser2.Url.AbsoluteUri == "http://www.clubvip.co.za/login.aspx") && (Properties.Settings.Default.BuyNowCount == 0))
            {
                HtmlElement browserUsername = webBrowser2.Document.GetElementById("ctl00_ContentPlaceHolder1_CellNumberRadText");
                browserUsername.InnerText = Properties.Settings.Default.Username;

                HtmlElement browserPassword = webBrowser2.Document.GetElementById("ctl00_ContentPlaceHolder1_PasswordRadText");
                browserPassword.InnerText = Properties.Settings.Default.Password;

                HtmlElement browserLoginButton = webBrowser2.Document.GetElementById("ctl00_ContentPlaceHolder1_cmdLogin");
                browserLoginButton.InvokeMember("click");

                Properties.Settings.Default.BuyNowCount = Properties.Settings.Default.BuyNowCount + 1;

                //itmBuyNow_Click(this, e);
                checkBuyNowStatus();

                return;
                                              
            }
            else if (webBrowser2.Url.AbsoluteUri == "http://www.clubvip.co.za/Members/Index.aspx")
            {
                webBrowser2.AllowNavigation = true;

                webBrowser2.Navigate("http://www.clubvip.co.za/Members/Shop.aspx");
            }

            else if (webBrowser2.Url.AbsoluteUri == "http://www.clubvip.co.za/Members/Shop.aspx")
            {
                shopSource = webBrowser2.DocumentText;
                checkBuyNow = manipulateShopSource(shopSource);

                if (checkBuyNow == true)
                {
                    MessageBox.Show("There is an item to Buy in the shop", "Buy Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (Properties.Settings.Default.CheckBuyNowStatus == true)
                {
                    //Need to make the itmBuyNow_Click a method where it is called,
                    //Have to do this so I know when the auto timer is calling it or being clicked to check now.

                    notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                    notifyIcon1.BalloonTipTitle = "No Item To Buy";
                    notifyIcon1.BalloonTipText = "There doesn't seem to be an item in the store to buy.\nPlease try again later.";
                    notifyIcon1.ShowBalloonTip(7000);
                    Properties.Settings.Default.CheckBuyNowStatus = false;
                }
            }

            Properties.Settings.Default.BuyNowCount = 0;
            
        }

        private bool manipulateShopSource(string source)
        {

            int shopStartIndex = source.IndexOf("Assets/Images/banners/burn_header.png");
            int shopEndIndex = source.IndexOf("Assets/Images/banners/buydraw.png");
            int shopStringLength = shopEndIndex - shopStartIndex;

            if ((shopStartIndex == -1) || (shopEndIndex == -1))
            {
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.BalloonTipTitle = "Shop Website Down";
                notifyIcon1.BalloonTipText = "Shop Website seems to be unreachable or modified.\nPlease contact the administrator.\nShop Polling will now deactivate.";
                notifyIcon1.ShowBalloonTip(7000);
                return false;
            }

            string shopItemsSource = source.Substring(shopStartIndex, shopStringLength);
            shopItemsSource.ToLower();
            
            //Static text for testing purposes.
            //string shopItemsSource = "Assets/Images/banners/burn_header.png\" width=\"825\" height=\"36\" alt\"\"/></div>\r\n  <table cellpadding=\"2\" cellspacing=\"2\" width=\"825\">\r\n  \r\n  \r\n<tr>\r\n<td style=\"padding-left:15px\">\r\n<img id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl00_imglink\" src=\"/images/shop/lionscri_thumbnail.jpg\" style=\"border-width:0px;\" />\r\n</td>\r\n<td>\r\n<b>\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl00_lblName\">Highveld Lions Hamper</span> \r\n</b>\r\n<br />\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl00_lblDesc\">The Highveld Lions will be taking on the Nashua Mobile Cape Cobras in the Standard Bank Pro20 on Sunday 30 January at the Bidvest Wanderers Stadium. Burn 19 000 points for you Highveld Lions Hamper, which includes 4 tickets to the game on Sunday 30 January, a Highveld Lions mug and USB heat mat to keep your mug warm, a mini cricket bat signed by the Highveld Lions team and other awesome goodies. Please note that you will need to collect your tickets from our offices, during office hours.</span> \r\n</td>\r\n<td>\r\n<div align=\"center\">\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl00_lblPoints\">19000</span>\r\n</span>\r\n   <br />\r\n <span>points</span></div></td><td><img alt=\"\"  src=\"../Assets/Images/buynow.gif\"></a>\r\n</td>\r\n\r\n</tr>\r\n\r\n<tr>\r\n<td style=\"padding-left:15px\">\r\n<img id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl01_imglink\" src=\"/images/shop/sean.jpg\" style=\"border-width:0px;\" />\r\n</td>\r\n<td>\r\n<b>\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl01_lblName\">Sean Kingston Tickets</span> \r\n</b>\r\n<br />\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl01_lblDesc\">A rapper at heart, Sean Kingston initially charmed music fans with the reggae-pop vocals on his breakthrough hits \"Beautiful Girls\" and “Fire Burning”. Don’t miss this International superstar live at Emperors Palace as he culminates the release of his third album, and comes off the back of two of the biggest hits of 2010 -  “Eeenie Meenie’ with Justin Bieber and \"Letting Go (Dutty Love)\" with Trinidadian rapper Nicki Minaj – a show not to be missed! Burn 10 000 points for 2 tickets to see Sean Kingston Live on Saturday 26 February at Emperors Palace.</span> \r\n</td>\r\n<td>\r\n<div align=\"center\">\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl01_lblPoints\">10000</span>\r\n</span>\r\n   <br />\r\n <span>points</span></div></td><td><img alt=\"\"  src=\"../Assets/Images/comingup.jpg\"></a>\r\n</td>\r\n\r\n</tr>\r\n\r\n<tr>\r\n<td style=\"padding-left:15px\">\r\n<img id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl02_imglink\" src=\"/images/shop/tangled.jpg\" style=\"border-width:0px;\" />\r\n</td>\r\n<td>\r\n<b>\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl02_lblName\">Tangled Hamper</span> \r\n</b>\r\n<br />\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl02_lblDesc\">Walt Disney Pictures presents \"Tangled,\" one of the most hilarious, hair-raising tales ever told. When the kingdom's most wanted-and most charming-bandit Flynn Rider (voice of Zachary Levi) hides out in a mysterious tower, he's taken hostage by Rapunzel (voice of Mandy Moore), a beautiful and feisty tower-bound teen with 70 feet of magical, golden hair. Burn 9 000 points for an exclusive Tangled hamper, including a Tangled watch, kids t-shirt and Tangled notebook.</span> \r\n</td>\r\n<td>\r\n<div align=\"center\">\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlcommingupunder_ctl02_lblPoints\">9000</span>\r\n</span>\r\n   <br />\r\n <span>points</span></div></td><td><img alt=\"\"  src=\"../Assets/Images/comingup.jpg\"></a>\r\n</td>\r\n\r\n</tr>\r\n\r\n        \r\n<tr>\r\n<td style=\"padding-left:15px\">\r\n<img id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlsoldoutunder_ctl00_imglink\" src=\"/images/shop/proteas.jpg\" style=\"border-width:0px;\" />\r\n</td>\r\n<td>\r\n<b>\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlsoldoutunder_ctl00_lblName\">Proteas Friday Hamper</span> \r\n</b>\r\n<br />\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlsoldoutunder_ctl00_lblDesc\">Similar to the successful trend of Football Fridays, South Africans are now being encouraged to show their 100% support for the Proteas ahead of the ICC Cricket World Cup in February. In support of Proteas Friday, Club VIP is giving away official ICC Crickets World Cup 2011 hampers, supplied by Promo-gear & Sneakers stores, who carry the largest range of supporters’ requirements. Burn 11 000 points for your hamper which includes a supporters shirt, cap and other great goodies.</span> \r\n</td>\r\n<td>\r\n<div align=\"center\">\r\n<span id=\"ctl00_ContentPlaceHolder1_rptRepeaterControlsoldoutunder_ctl00_lblPoints\">11000</span>\r\n</span>\r\n   <br />\r\n <span>points</span></div></td><td><img alt=\"\" src=\"../Assets/Images/soldout.jpg\"></a>\r\n</td>\r\n\r\n</tr>\r\n\r\n        \r\n \r\n \r\n  </table>\r\n\r\n\r\n                                         \r\n<div class=\"TextSpc\"><img src=\"../";


            int buynowIndex = shopItemsSource.IndexOf("buynow.gif");

            if (buynowIndex == -1)
            {
                buynowIndex = shopItemsSource.IndexOf("buynow.jpg");

                if (buynowIndex == -1)
                {
                    buynowIndex = shopItemsSource.IndexOf("buynow.jpeg");

                    if (buynowIndex == -1)
                    {
                        buynowIndex = shopItemsSource.IndexOf("buy");

                        if (buynowIndex == -1)
                        {                            
                            return false;
                        }
                        else
                        {
                            return true;
                        }                        
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //itmBuyNow_Click(this, e);
            checkBuyNowStatus();
        }

        private void itmBuyNowTimer_Click(object sender, EventArgs e)
        {
            Form setBuyNowTimer = new SetBuyNowTimer();
            setBuyNowTimer.Show();
        }

        private void turnOffTimerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (turnOffTimerToolStripMenuItem.Checked == false)
            {
                turnOffTimerToolStripMenuItem.Checked = true;
                timer2.Enabled = false;

                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                notifyIcon1.BalloonTipTitle = "Shop Timer Disabled";
                notifyIcon1.BalloonTipText = "The Shop Timer has been disabled and will not check for buy now items.";
                notifyIcon1.ShowBalloonTip(7000);
            }
            else
            {
                turnOffTimerToolStripMenuItem.Checked = false;
                timer2.Enabled = true;

                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                notifyIcon1.BalloonTipTitle = "Shop Timer Enabled";
                notifyIcon1.BalloonTipText = "The Shop Timer has been Enabled and will now check for buy now items.";
                notifyIcon1.ShowBalloonTip(7000);
            }

            
        }

        private bool checkBuyNowStatus()
        {
            try
            {
                webBrowser2.AllowNavigation = true;

                webBrowser2.Navigate("http://www.clubvip.co.za/Members/Shop.aspx");

                return true;
            }
            catch
            {
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                notifyIcon1.BalloonTipTitle = "Shop Website Error";
                notifyIcon1.BalloonTipText = "There seems to be an error with the Shop Website.\nPlease contact the administrator.\nShop Polling will now deactivate.";
                notifyIcon1.ShowBalloonTip(7000);
                timer2.Enabled = false;
                return false;
            }
        }
    }
}
